import requests
import parsel
import csv
from lxml import etree

for paeg in range(0,11):
    print(f"第{paeg}页正在爬取————————————————————————————————————————————————————")

    # url = f'https://movie.douban.com/subject/35613853/comments?start={paeg}&limit=20&status=P&sort=time'
    url = f'https://movie.douban.com/subject/35613853/comments?start=0&limit=20&status=P&sort=time'

    head = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'
    }
    data_html = requests.get(url, headers=head).text

    # print(data_html)

    senlector = parsel.Selector(data_html)
    comment_list = senlector.css('.comment-item ')
    for comment in comment_list:
        name = comment.css('.comment-info a::text').get().strip()
        short = comment.css('.short::text').get().strip()
        time = comment.css('.comment-time::text').get().strip()
        vote_count = comment.css('.votes.vote-count::text').get().strip()
        print(name, short, time, vote_count)

        paeg = + 20

        with open('长津湖.csv', mode='a', encoding='utf-8', newline='') as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow([name, short, time, vote_count])